package com.rgsx.controller;

import com.rgsx.entiy.Position;
import com.rgsx.service.UserService;
import com.rgsx.utils.FileUtil;
import com.rgsx.utils.JWTUtil;
import com.rgsx.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

@RestController
@ResponseBody
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户相关的API")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登陆",notes = "语音输入")
    public ResultVo login(@RequestParam(value = "file") MultipartFile multipartFile){
        File file = FileUtil.MultipartFileToFile(multipartFile);
//        fileWrite(file);
        return userService.login(file);
    }

    @PostMapping("/login2")
    @ApiOperation(value = "密码登陆",notes = "密码登陆")
    public ResultVo login2(String userName , String password, HttpServletResponse response){
        Map<String, Object> result = null;
        try {
            result = userService.login2(userName, password);
            if(result == null){
                return ResultVo.fail("登陆失败","1");
            }
            response.setHeader("token", (String) result.get("token"));
            return ResultVo.success("成功登陆",result.get("user"));
        } catch (Exception e) {
            return ResultVo.fail(e);
        }
    }

    @PostMapping("/register2")
    @ApiOperation(value = "注册",notes = "注册")
    public ResultVo register2(String uuid,String code,String password,int identity){
        return userService.register2(uuid,code,password,identity);
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "用户信息",notes = "用户信息")
    public ResultVo getUserInfo(@RequestHeader("token") String token){
        if(token == null)return ResultVo.fail("登陆过期","-1");
        return userService.getUserInfo(token);
    }

    @GetMapping("/setUserLocation")
    @ApiOperation(value = "设置用户位置",notes = "设置用户位置")
    public ResultVo setUserLocation(@RequestHeader("token") String token,
                                    String positionLatitude,
                                    String positionLongitude){

        long userId = JWTUtil.getUserId(token);
        Position position = new Position(positionLatitude,positionLongitude);
        return userService.setUserLocation(userId,position);
    }

    @GetMapping("/sendVerificationCode")
    @ApiOperation(value = "发送验证码",notes = "发送验证码")
    public ResultVo sendVerificationCode(String phoneNumber){
        return userService.sendVerificationCode(phoneNumber);
    }




    //测试专用 - 音频写入window本地
    private static void fileWrite(File file){
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;
        byte[] bs = new byte[1024];
        int len;
        try {
            fileOutputStream = new FileOutputStream(new File("D:\\test.wav"));
            fileInputStream = new FileInputStream(file);

            while ((len = fileInputStream.read(bs)) != -1) {
                fileOutputStream.write(bs, 0, len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
